import { ChatMessageHistory } from "langchain/stores/message/in_memory";
import {
  HumanMessage,
  AIMessage,
  isAIMessage,
  isHumanMessage,
} from "@langchain/core/messages";

const history = new ChatMessageHistory();

await history.addMessage(new HumanMessage("你好，我叫小明"));
await history.addMessage(new AIMessage("你好，小明！有什么我可以帮助你的吗？"));

const messages = await history.getMessages();

messages.forEach((msg, i) => {
  const role = isHumanMessage(msg)
    ? "用户"
    : isAIMessage(msg)
    ? "AI"
    : "❓ Unknown";

  console.log(`[${i + 1}] ${role} ➜ ${msg.content}`);
});
